<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>银行业务系统登录</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <style>
        body {
            background: #e9ecef;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            width: 100%;
            max-width: 500px;
            padding: 40px;
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
        }

        .login-title {
            margin-bottom: 30px;
            text-align: center;
            color: #007bff;
        }

        .form-control {
            height: 45px;
            font-size: 16px;
        }

        .btn-login {
            height: 45px;
            font-size: 16px;
        }
    </style>
</head>
<body>

<div class="login-container">
    <h3 class="login-title">银行业务系统登录</h3>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" required>
        </div>
        <button type="submit" class="btn btn-primary btn-block btn-login">登录</button>
    </form>
    <div class="alert alert-danger mt-3 d-none" id="errorMsg"></div>
</div>

<!-- JS -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<script>
    $('#loginForm').on('submit', function (e) {
        e.preventDefault(); // 阻止默认提交
        const username = $('#username').val();
        const password = $('#password').val();

        $.ajax({
            url: '/auth/login',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({ username, password }),
            success: function (res) {
                if (res.code === 200) {
                    const role = res.data.role;
                    localStorage.setItem("token", res.data.token); // 保存 token
                    if (role === 'ADMIN') {
                        window.location.href = '/admin/index';
                    } else if (role === 'CUSTOMER') {
                        window.location.href = '/customer/index';
                    } else {
                        showError('未知角色，无法跳转');
                    }
                } else {
                    showError(res.msg || '登录失败');
                }
            },
            error: function () {
                showError('服务器异常，请稍后重试');
            }
        });
    });

    function showError(msg) {
        $('#errorMsg').text(msg).removeClass('d-none');
    }
</script>
</body>
</html>